/* 防御塔原型扩展 */

import { getDistance, isInArray } from "@/utils"
import creep from "../creep"

export default class TowerExtension extends StructureTower {
    /*
    ** 防御塔工作A型1号
    ** 1. 战争状态下不修复建筑，仅攻击敌人
    ** 2. 其他状态下，优先修复道路，敌人存在5tick后才开始攻击
    ** 3. 所有的攻击状态：如果存在大于等于1个敌人，则优先攻击携带治疗的敌人
    */
    public work_A1():void{
        if (!this.room.memory.roomSTATE) return
        if (this.room.memory.roomSTATE != 'war')
        {
            if (! (Game.time % 2)){
                var roads = this.pos.getRangedStructure([STRUCTURE_ROAD,STRUCTURE_CONTAINER],40,2) as StructureRoad
            if (roads)
            if (roads.hitsMax - roads.hits >= 800)
                this.repair(roads)
            }
            else
            {
                if (Game.flags[`${this.room.name}/pause`]) return
                var leastRampart = this.room.getListHitsleast([STRUCTURE_RAMPART,STRUCTURE_WALL],3)
                if (!leastRampart) return
                if (leastRampart.hits < 10000000)
                {
                    this.repair(leastRampart)
                }
                var creeps = this.room.find(FIND_MY_CREEPS,{filter:(creep)=>{
                    return creep.hits < creep.hitsMax
                }})
                if (creeps.length > 0)
                    this.heal(creeps[0])
            }
        }
        else
        {
            var enemys = this.room.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            this.attack_01(enemys)
        }
    }
    /*
    ** 防御塔工作A型2号
    ** 1.永远不参与修复，仅进行攻击，而且优先攻击携带heal爬虫,敌人存在3tick后开始攻击
    */
    public work_A2():void{
        if (this.room.memory.roomSTATE == 'peace') return
        var enemys = this.room.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
            return !isInArray(Memory.whitesheets,creep.owner.username)
        }})
        for (var i of enemys)
        {
            if (i.owner.username == 'Invader')
            {
                this.attack(i)
                return
            }
        }
        /* 不是npc的情况 */
        if (!Game.flags[`${this.room.name}/Tower`])
        {
            // if (Game.flags[`${this.room.name}/Heal`] && this.room.CheckInMisson('CREEP','红球防御'))
            // {
            //     var task = this.room.getMisson('CREEP','红球防御')
            //     if (Object.keys(task.bindData).length > 0)
            //     {
            //         var Rcreep = Game.creeps[Object.keys(task.bindData)[0]]
            //         if (Rcreep && Rcreep.hits < Rcreep.hitsMax) this.heal(Rcreep)
            //         else
            //         {
            //             var injuredCreep = this.pos.findClosestByRange(FIND_MY_CREEPS,{filter:(creep)=>{
            //                 return creep.hits < creep.hitsMax
            //             }})
            //             if (injuredCreep) this.heal(injuredCreep)
            //         }
            //         return
            //     }
            // }
            if (this.room.memory.roomSTATE == 'war' &&  Game.time % 5 == 0)
            {
                var dis_creep:Creep = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                    return !isInArray(Memory.whitesheets,creep.owner.username)
                }})
                this.attack(dis_creep)
            }
            else
            {
                if (Game.flags[`${this.room.name}/Repair`])
                {
                    var dis_stru = Game.flags[`${this.room.name}/Repair`].pos.getClosestStructure(['constructedWall','rampart'],0)
                    this.repair(dis_stru)
                }
            }

        }
        else
        {
            var dis_creep:Creep = Game.flags[`${this.room.name}/Tower`].pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            this.attack(dis_creep)
        }
    }
    /* 1型号攻击函数，优先攻击治疗虫 参数为列表*/
    public attack_01(creeps:Creep[]):void
    {
        if (creeps.length == 0) return
        if (creeps.length == 1) this.attack(creeps[0])
        if (creeps.length > 1)
        {
            // 如果有治疗虫，则优先攻击治疗数量最多的虫子，否则按照就近原则攻击
            var dis_creep:Creep = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            var dis_creep2 = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            for (var i of creeps)
            {
                // 如果携带的治疗数量大于dis_creep，dis_creep就换成该目标
                if (i.getActiveBodyparts(HEAL) > dis_creep.getActiveBodyparts(HEAL))
                    dis_creep = i
                if (i.getActiveBodyparts(ATTACK) > 0)
                    dis_creep2 = i
            }
            if (getDistance(this.pos,dis_creep.pos) <= 18)
            if(Game.time % 2)
            this.attack(dis_creep)
            else
            {
                if (dis_creep2)
                    this.attack(dis_creep2)
            }
            
        }
    }
}